package com.cat.dynamicPlanning02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-subarray-with-bitwise-or-closest-to-k/
 * @create 2025/11/5 19:46
 * @since JDK17
 */

public class Solution44 {
    public int minimumDifference(int[] nums, int k) {
        int ans = Integer.MAX_VALUE, n = nums.length;
        for (int i = 0; i < n; i++) {
            int x = nums[i];
            ans = Math.min(ans, Math.abs(x - k));
            for (int j = i - 1; j >= 0 && ((nums[j] | x) != nums[j]); j--) {
                nums[j] |= x;
                ans = Math.min(ans, Math.abs(nums[j] - k));
            }
        }

        return ans;
    }
}
